from django.core.management.base import BaseCommand, CommandError
from apps.order.models import *
from apps.message.models import *
from apps.product.models import *
from apps.repo.models import *
from datetime import timedelta
import time
from django.db import transaction
import sys
import time


class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('order_id', type=str)

    def handle(self, *args, **options):
        order_id = options.get('order_id')

        order = Order.objects.get(pk=order_id)

        sub_orders = SubOrder.objects.filter(order=order)
        market_order_ids = [sub_order.market_order_id for sub_order in sub_orders]
        market_order = MarketOrder.objects.filter(id__in=market_order_ids).first()

        if market_order and market_order.intention_type == MarketOrder.IntentionType.TAKE_OUT.name:
            # GoodsBlock.objects.filter(market_order_id__in=market_order_ids).update(market_order=None,
            #                                                                        trade_type=GoodsBlock.TradeType.MARKET.name)
            market_order.buy_in_market_order = None
            market_order.soft_delete()

        GoodsBlock.objects.filter(id__in=[so.goods_block_id for so in sub_orders]).update(
            market_order=None, trade_type=GoodsBlock.TradeType.MARKET.name, taken_out=False)

        SubOrder.objects.filter(order=order).update(deleted_at=time.time())
        order.soft_delete()
        # Payment.objects.filter(order=order).update(deleted_at=time.time())
